package com.impetus.project.skilltest.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.impetus.project.skilltest.core.SkillTestException;
import com.impetus.project.skilltest.dataobject.SystemProperty;
import com.impetus.project.skilltest.util.HibernateUtil;

public class SystemPropertiesDAO {

		private static Logger logger = Logger.getLogger(SystemPropertiesDAO.class.getName());
		
		private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		
		@SuppressWarnings("unchecked")
		public static List<SystemProperty> getSystemPropertyByName ( String propertyName )
		throws SkillTestException
		{
			List<SystemProperty> systemPropertyList = new ArrayList<SystemProperty>();
			
			try{
				
				systemPropertyList = sessionFactory.getCurrentSession()
				.createCriteria(SystemProperty.class)
				.add(Restrictions.like("PROPERTY_NAME", propertyName))
				.add(Restrictions.like("IS_ENABLED", "Y"))
				.list();
				
			}catch(Exception e)
			{
				logger.log(Level.INFO, e.getMessage());
				throw new SkillTestException(e);
			}			
			
			return systemPropertyList;
		}
		
		public static void saveUpdateSystemProperty(SystemProperty systemProperty)
		throws SkillTestException
		{
			try
			{
				sessionFactory.getCurrentSession().saveOrUpdate(systemProperty);
			
			}catch(Exception e)
			{
				logger.log(Level.INFO, e.getMessage());
				throw new SkillTestException(e);
			}
		}	
		
		public static void deleteSystemProperty(SystemProperty systemProperty)
		throws SkillTestException
		{
			try
			{
				sessionFactory.getCurrentSession().delete(systemProperty);
				
			}catch(Exception e)
			{
				logger.log(Level.INFO, e.getMessage());
				throw new SkillTestException(e);
			}
		}
}
